// 用户数据模块
import store from '@/store'
import {
	sendMessage
} from '@/api/websocket.js'

import $api from '@/api/flag.js'

const state = {
	walletInfo: uni.getStorageSync('walletInfo') || null
}

const mutations = {
	saveWalletInfo(state, data) { // 钱包信息
		console.log(data)
		uni.setStorageSync('saveWalletInfo', data)
		state.walletInfo = data
	},
}

const actions = {
	getWalletInfo({
		commit,
		state,
		getters
	}) {
		return new Promise(callback => {
			const {
				USERWALLET
			} = $api
			sendMessage({
				flag: USERWALLET,
			})
			uni.$once(USERWALLET, (res) => {
				const {
					code,
					data
				} = res
				if (code === 1) {
					callback(data)
					commit('saveWalletInfo', data)
				}
				callback(false)
			})
		})
	}
}

const getters = {
	isLogin(state) {
		return uni.getStorageSync('token') ? true : false;
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions,
	getters
}